package com.vernhe.list;


public class LinkedList<E> extends AbstractList<E> {
    private Node first;
    private Node last;

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }
    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E old = node.element;
        node.element = element;
        return old;
    }

    /**
     * 添加节点
     * @param index
     * @param element
     */
    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        //index == size
        if (index == size) {
            Node<E> oldLast = last;
            last = new Node<E>(oldLast,element,null);
            if (oldLast == null) {
                first = last;
            } else {
                oldLast.next = last;
            }
        } else {
            Node<E> next = node(index);
            Node<E> prev = next.prev;
            Node<E> node = new Node<>(prev,element,next);
            next.prev = node;
            // index == 0
            if (prev == null) {
                first = node;
            }else {
                prev.next = node;
            }
        }
        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node node = node(index);
        Node prev = node.prev;
        Node next = node.next;
        //index == 0
        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
        }
        //index == size - 1
        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
        }
        size--;
        return (E) node.element;
    }

    @Override
    public int indexOf(E element) {
        if (element == null){
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;
                node = node.next;
            }
        }else {
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) return i;
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    /**
     * 返回索引位置的节点
     * @param index
     * @return
     */
    private Node<E> node(int index) {
        rangeCheck(index);
        if (index < (size >> 1)) {
            Node<E> node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        } else {
            Node<E> node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.prev;
            }
            return node;
        }
    }

    private static class Node<E> {
        Node prev;
        E element;
        Node next;

        public Node(Node prev,E element, Node next) {
            this.prev = prev;
            this.element = element;
            this.next = next;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            if (prev == null) {
                sb.append("null");
            }else {
                sb.append(prev.element);
            }
            sb.append("<-").append(element).append("->");
            if (next == null) {
                sb.append("null");
            }else {
                sb.append(next.element);
            }

            return sb.toString();
        }
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        Node<E> node = first;
        string.append("size=" + size).append(",[");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(",");
            }
            string.append(node);
            node = node.next;
        }
        string.append("]");

//        while (node != null) {
//            node = node.next;
//        }
        return string.toString();
    }
}
